package com.ericsson.enap.modules.replay.task;


import com.ericsson.enap.modules.replay.entity.EpcReplayProjectFnumber;
import com.ericsson.enap.modules.replay.entity.EpcReplayRfnumberTree;
import com.ericsson.enap.modules.replay.entity.EpcReplayTestProject;
import com.ericsson.enap.modules.replay.service.IEpcReplayFnumberTreeService;
import com.ericsson.enap.modules.replay.service.IEpcReplayProjectFnumberService;
import com.ericsson.enap.modules.replay.service.IEpcReplayRfnumberTreeService;
import com.ericsson.enap.modules.replay.service.IEpcReplayTestProjectService;
import com.ericsson.common.util.SocketClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReplayTestTask {


    @Autowired
    private SocketClient socketClient;

    @Autowired
    private IEpcReplayTestProjectService epcReplayTestProjectService;

    @Autowired
    private IEpcReplayProjectFnumberService epcReplayProjectFnumberService;

    @Autowired
    private IEpcReplayRfnumberTreeService epcReplayRfnumberTreeService;

    @Autowired
    private IEpcReplayFnumberTreeService epcReplayFnumberTreeService;

    public void execute(long testProjectId,long frequency,int total,String startTime,String serverIp,int port) throws Exception {
        //1、查询测试项目信息
        EpcReplayTestProject epcReplayTestProject = epcReplayTestProjectService.selectEpcReplayTestProjectById(testProjectId);
        //2、查询测试项目下帧流程图列表
        List<EpcReplayProjectFnumber> epcReplayProjectFnumbers = epcReplayProjectFnumberService.selectEpcReplayProjectFnumberByProjectId(String.valueOf(testProjectId));
        //3、处理第一帧
        String sendRequestContent = null;
        String frameName = null;
        String firstFrameId = null;
        for(EpcReplayProjectFnumber temp:epcReplayProjectFnumbers){
            if("0".equals(temp.getPid())){
                sendRequestContent = temp.getRequestContent();
                firstFrameId = temp.getId();
                frameName = temp.getFrameName();
                break;
            }
        }
        socketClient.initSocket(serverIp, port);
        String serverResult = socketClient.sendMessage("==== client ==== 客户端发送帧:"+ frameName + "  " + sendRequestContent);
        System.out.println("==== server ==== : " + serverResult);
        //4、发第二帧...
        checkAndSentNextFrame(serverIp, port, epcReplayProjectFnumbers, firstFrameId, serverResult);
        socketClient.close();
    }

    private void checkAndSentNextFrame(String serverIp, int port, List<EpcReplayProjectFnumber> epcReplayProjectFnumbers, String thisFrameId,
                                       String serverResult) throws Exception {
        for(EpcReplayProjectFnumber temp:epcReplayProjectFnumbers){
            if(thisFrameId.equals(temp.getPid())){
                if(temp.getResponseFnumber()==null){//4.1、处理选择“不处理上一帧响应的情况”,如果遇见直接break;
                    socketClient.initSocket(serverIp, port);
                    socketClient.sendMessage("==== client ==== 客户端发送帧:"+ temp.getFrameName() + "  " + temp.getRequestContent());
                    break;
                }else{
                    String tempFrameId = temp.getId();
                    EpcReplayRfnumberTree epcReplayRfnumberTree = new EpcReplayRfnumberTree();
                    epcReplayRfnumberTree.setIsCheck(1L);
                    epcReplayRfnumberTree.setNodeId(tempFrameId);
                    List<EpcReplayRfnumberTree> epcReplayRfnumberTrees = epcReplayRfnumberTreeService.selectEpcReplayRfnumberTreeList(epcReplayRfnumberTree);
                    //根据check项截取serverResult，判断所有返回帧检查项是否一致，若一致则马上执行此分支请求
                    boolean flag = true;
                    for(EpcReplayRfnumberTree tree:epcReplayRfnumberTrees){
                        int start = (int) (tree.getStartPosition()*2);
                        int end = (int) (tree.getStartPosition()*2+tree.getColLen()*2);
                        String str = serverResult.substring(start,end);
                        if(!str.equals(tree.getColValue16())){
                            flag = false;
                            break;
                        }
                    }
                    if(flag){//只有检查成功了才能继续下一帧
                        socketClient.initSocket(serverIp, port);
                        socketClient.sendMessage("==== client ==== 客户端发送帧:"+ temp.getFrameName() + "  " + temp.getRequestContent());
                        //递归调用方法
                        checkAndSentNextFrame(serverIp, port, epcReplayProjectFnumbers, tempFrameId, serverResult);
                        break;
                    }else{
                        continue;
                    }
                }
            }
        }
    }
}
